/**
 * 微信 主应用入口
 *
 */
define(["public/app.min"], function () {

    var self,
        app = win.app,
        types = [],
        units = [],//单位
        zhiwei = [],//职位
        juese = [],//角色
        menus = [],
        others = {
            hasgwc: true
        },
        statsSJ = {
            A: 0, B: 0, C: 0
        },
        keyword,
        sData={
            keyword:''
        },
        page = {
            commentCount: 0,
            orderCount: 0,
            serverCount: 0,
            current: 1,
            totalNum: 0,
            fwyyList: [],
            count: {},
            hotList: [],
            promotsList: []
        }
    ;

    function getUserInfoBack() {

        menus.pushArray(app.userInfos.auths, true);

        menus.iFilter({
            o: {
                permission_: 'wx0006'
            },
            cb: function (arr, i) {
                if (i > 0) {
                    others.hasgwc = true;
                }
            }
        });

        self.getFwyy();
    }

    app.newModule({
        init: function () {
            self = this;

            self._super({
                _tag: 'p_outer-user',
                _search: {
                    data:sData,
                    methods: {
                        clickIn: function() {
                            var getFirst = document.getElementsByClassName('noneFouce')[0];
                            var getLast = document.getElementsByClassName('hasFouce')[0];
                            getFirst.style.display = 'none';
                            getLast.style.display = 'block';
                            getLast.focus();
                        },
                        lostIn: function(e) {
                            //console.log(e,sData.keyword)
                            var getFirst = document.getElementsByClassName('noneFouce')[0];
                            var getLast = document.getElementsByClassName('hasFouce')[0];

                            if(sData.keyword == '') {
                                getFirst.style.display = 'block';
                                getLast.style.display = 'none';
                            }

                        },
                        scanQRCode: function () {
                            app.scanQRCode(function (code) {
                                self.openGoodsInfo(code)
                            })
                        },
                    }
                },
                _readyBefore: function () {
                    //最先执行
                    self.mData = {
                        authData: [],
                        obj: {
                            dqs: 0,
                            wfh: 0
                        }
                    };

                    app.mainPage = {};

                    self.zhiwei = zhiwei;//职位
                    self.juese = juese;//角色

                }
            }, win);

        },
        _search: function (data) {

            keyword = data.keyword.trim();
            self.openGoodsInfo(keyword);

        },
        statsSJ: function () {
            app.ajax({
                xurl: "api/wx/gwaq554/statsSJ",
                cb: function (result) {
                    var res = result.data;
                    if (result.ok) {
                        var data = res.data;
                        data.GWAQ554.GWCS039 = (data.GWAQ554.GWCS039 / 10000).toFixed(2);
                        data.GWAQ555.GWBK005 = (data.GWAQ555.GWBK005 / 10000).toFixed(2);
                        statsSJ['A'] = data.GWAQ554.GWCS039;
                        statsSJ['B'] = data.GWAQ555.GWBK005;
                        statsSJ['C'] = data.GWAQ556;
                    }
                }
            });
        },
        setLocation: function () {
            let latlng = app.wxLocationCache();
            if (latlng.lat) {
                app.lnglatConvert({
                    lat: latlng.lat,// 纬度
                    lng: latlng.lng,// 经度
                    cb: function (lnglat) {
                        app.ajax({
                            xurl: "api/wx/gwa/setLocation",
                            xdata: {
                                GWAA020: lnglat.lat,// 纬度
                                GWAA019: lnglat.lng// 经度
                            }
                        });
                    }
                });
            }
        },
        getZhiwei: function () {
            app.ajax({
                xurl: "api/wx/gwa/getZw",
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data;
                        zhiwei.pushArray(data.data, true);
                    }
                }
            });
        },
        getJuese: function () {
            app.ajax({
                xurl: "api/wx/gwa/getJs",
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data;
                        juese.pushArray(data.data, true);
                    }
                }
            });
        },
        getUnits: function () {
            app.ajax({
                xurl: "api/wx/gwa/getGwablList",
                ocache: true,
                xtime: '1d',
                cb: function (result) {
                    if (result.ok) {
                        units.pushArray(result.data.data);
                    }
                }
            });
        },
        getType: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getGwahData",
                cb: function (result) {
                    if (result.ok) {
                        types.pushArray(result.data.data);
                    }
                }
            });
        },
        dataDrawing: function () {


            //执行数据渲染
            new Vue({
                el: '#content',
                data: page,
                methods: {
                    go: function (type, v) {
                        switch (type) {
                            case 'hot':
                                app.ajax({
                                    xurl: 'api/wx/outerUser/getGoodsInfo',
                                    xdata: {
                                        keyword: v.GWAM001
                                    },
                                    cb: function (res) {
                                        if (res.ok) {
                                            app.openPage({
                                                src: "gwcxd/classes-info.html",
                                                argsKey: 'classesinfo_page',
                                                args: {
                                                    item: app.cloneJSON(res.data.data[0]),
                                                }
                                            });
                                        }
                                    }
                                })
                                break;
                            case 'ddmx':
                                app.openPage({
                                    argsKey: 'p_main',
                                    src: "gwat310/gwat310.html",
                                    args: {
                                        userInfo: self.userinfo
                                    }
                                });
                                break;
                            case 'fw':
                                app.openPage({
                                    src: "jdcx/jdcx.html",
                                });
                                break;
                            case 'pj':
                                app.openPage({
                                    src: "fwpj/fwpj.html",
                                });
                                break;
                        }
                    },
                    openFL: function () {
                        app.openPage({
                            src: "gwcxd/classes.html"
                        });
                    },
                },
                updated: function () {
                    mui(".mui-slider").slider({
                        interval: 2000
                    });
                }
            });

            new Vue({
                el: '#promotesDiv',
                data: {
                    promotes: page.promotsList
                },
                methods: {
                    openDetail: function (v) {
                        app.ajax({
                            xurl: 'api/wx/outerUser/getGoodsInfo',
                            xdata: {
                                keyword: v.GWAML001
                            },
                            cb: function (res) {
                                if (res.ok) {
                                    app.openPage({
                                        src: "gwcxd/classes-info.html",
                                        argsKey: 'classesinfo_page',
                                        args: {
                                            item: app.cloneJSON(res.data.data[0]),
                                        }
                                    });
                                }
                            }
                        })
                    }
                },
                updated: function () {
                    mui("#promotesDiv").scroll({
                        deceleration: 0.0005,
                        scrollX: true,
                        scrollY: false
                    });
                }
            });

            new Vue({
                el: '#fwyycard',
                data: {
                    fwyyList: page.fwyyList
                },
                methods: {
                    openfwyy: function () {
                        app.openPage({
                            src: "jdcx/jdcx.html",
                            args: {}
                        });
                    }
                },
                updated: function () {

                }
            });
        },
        initFilters: function () {
            // 声明全局过滤器
            Vue.filter('MMRD001STATUS', function (t) {
                return win.eval({'1': '安装', '2': '维修', '3': '保养'})[t];
            });

            Vue.filter('MMRD019STATUS', function (t) {
                return win.eval({'1': '紧急', '2': '一般'})[t];
            });

            Vue.filter('sexStatus', function (t) {
                return win.eval({'0': '未知', '1': '男', '2': '女'})[t];
            });

            Vue.filter('MMRD020STATUS', function (t) {
                return win.eval({'1': '发起', '2': '受理', '3': '派工', '4': '报工', '5': '评价', '6': '回访结案'})[t];
            });

            Vue.filter('GWBJ005STATUS', function (t) {
                return win.eval({'1': '主要对手', '2': '重要对手', '3': '一般对手', '4': '其他对手'})[t];
            });

            Vue.filter('goodsUnits', function (t) {
                var a = units.filterKV({'GWABL001': t});
                if (a.length > 0) {
                    return a[0].GWABL003;
                } else {
                    return '未知';
                }
            });

            Vue.filter('zhiwei', function (t) {
                var a = zhiwei.filterKV({'GWBD001': t});
                if (a.length > 0) {
                    return a[0].GWBDL003;
                } else {
                    return '未知';
                }
            });

            Vue.filter('juese', function (t) {
                var a = juese.filterKV({'GWAW001': t});
                if (a.length > 0) {
                    return a[0].GWAWL003;
                } else {
                    return '未知';
                }
            });

            Vue.filter('goodsImgsUtils', function (name) {
                if (name) {
                    var origin = location.origin;

                    if (origin.indexOf('localhost') == -1) {
                        return location.origin + '/wissun-smfile/' + name.split(",")[0];
                    } else {
                        return 'http://fx.digiwin.com:9017/wissun-smfile/' + name.split(",")[0];
                    }
                } else {
                    return '../../../img/wx/images.png';
                }
            });

            Vue.filter('orderImgsUtils', function (name) {
                if (name) {
                    if (name.indexOf('wx-uploads') != -1) {
                        return app.cxt() + name;
                    } else {
                        return app.cxt() + 'wx-uploads/imgs/' + name;
                    }
                } else {
                    return '../../static/img/images.png';
                }
            });

            Vue.filter('dateFmt', function (date) {
                if (date) {
                    return date.replace(" 00:00:00", "")
                }
                return ""
            });
        },
        openGoodsInfo: function (code) {
            app.openPage({
                src: "common/selectSearchPage.html",
                args: {
                    url: 'api/wx/outerUser/getGoodsInfo',
                    keyword: code,
                    nodata: '无该商品',
                    keyArray: ['GWAML003', 'GWAM001'],
                    cb: function (res) {
                        app.openPage({
                            src: "gwcxd/classes-info.html",
                            argsKey: 'classesinfo_page',
                            args: {
                                item: app.cloneJSON(res),
                            }
                        });
                    }
                }
            });
        },
        getProducts: function () {

            app.ajax({
                xurl: 'api/wx/outerUser/gethot',
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data;
                        var hots = data.hot;

                        //爆款轮播图
                        if (hots.length > 0) {

                            page.hotList.push({
                                path: app.getImgUrl()+hots[hots.length - 1].GWAM015,
                                GWAM001: hots[hots.length - 1].GWAM001,
                                GWAML003: hots[hots.length - 1].GWAML003,
                                className: 'mui-slider-item mui-slider-item-duplicate'
                            });
                            hots.forEach(function (d, index) {
                                page.hotList.push({
                                    path: app.getImgUrl()+d.GWAM015,
                                    GWAM001: d.GWAM001,
                                    GWAML003: d.GWAML003,
                                    className: 'mui-slider-item'
                                })
                            });

                            page.hotList.push({
                                path: app.getImgUrl()+hots[0].GWAM015,
                                GWAM001: hots[0].GWAM001,
                                GWAML003: hots[0].GWAML003,
                                className: 'mui-slider-item mui-slider-item-duplicate'
                            });

                        }
                    }
                }
            });

            app.ajax({
                xurl: 'api/wx/outerUser/getPromotes',
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data;

                        var promots = data.promots;
                        //促销轮播图
                        if (promots.length > 0) {
                            promots.forEach(function (d, index) {
                                page.promotsList.push({
                                    path: app.getImgUrl()+d.GWAM015,
                                    GWAML001: d.GWAML001,
                                    dateStr: d.GWDC010.split(' ')[0],
                                    price: d.GWDCZ005 + '元/' + d.GWABL003
                                })
                            })
                        }
                    }
                }
            })
        },
        getCount: function () {
            app.ajax({
                xurl: 'api/wx/outerUser/getCount',
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        page.count = result.data.data;
                    }
                }
            })
        },
        getFwyy: function () {
            app.ajax({
                xurl: 'api/wx/outerUser/getfwyy',
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        page.fwyyList = result.data.data;
                    }
                }
            })
        },
        _ready: function () {

            self.dataDrawing();

            if (app.userInfos) {
                getUserInfoBack();
            } else {
                app.getUserInfoBack = function () {
                    getUserInfoBack();
                };
            }

            app.getImgUrl = function () {
                var origin = location.origin;
                if (origin.indexOf('localhost') == -1) {
                    return location.origin + '/wissun-smfile/';
                } else {
                    return 'http://fx.digiwin.com:9017/wissun-smfile/';
                }
            };

            self.getProducts();
            self.getCount();

        },
        _lazy: function () {
            this._super();

            self.initFilters();

            //self.statsSJ();

            self.getUnits();
            self.getJuese();
            self.getZhiwei();

            self.getType();

            new Vue({
                el: '#foot',
                data: {
                    keyword: keyword
                },
                methods: {
                    openFoot: function (t) {
                        if (t == 'fl') {
                            app.openPage({
                                src: "gwcxd/classes.html"
                            });
                        } else if (t == 'gwc') {
                            app.openPage({
                                src: "gwcxd/gwc.html",
                                argsKey: 'classes_page', //替换已经有的参数为空

                            });
                        } else if (t == 'wo') {
                            app.openPage({
                                src: "me/me.html",
                                args: {
                                    auths: menus
                                }
                            });
                        }
                    }

                }
            });

        }
    });

});